package com.sicpa.system.service.impl;

import com.sicpa.common.config.FtpConfig;
import com.sicpa.common.core.domain.entity.AntiCounterfeitingUnifiedFile;
import com.sicpa.common.ftp.FtpConnection;
import com.sicpa.common.ftp.FtpFactory;
import com.sicpa.common.utils.DateUtils;
import com.sicpa.common.utils.file.FileUploadUtils;
import com.sicpa.system.mapper.AntiCounterfeitingUnifiedFileMapper;
import com.sicpa.system.service.IAntiCounterfeitingUnifiedFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

/**
 * AntiCounterfeitingUnifiedFileServiceImpl
 *
 * @author lwf
 */
@Service
public class AntiCounterfeitingUnifiedFileServiceImpl implements IAntiCounterfeitingUnifiedFileService {

    private Logger log = LoggerFactory.getLogger(AntiCounterfeitingUnifiedFileServiceImpl.class);

    private AntiCounterfeitingUnifiedFileMapper antiCounterfeitingUnifiedFileMapper;

    private FtpFactory ftpFactory;

    private FtpConfig ftpConfig;

    @Autowired
    public AntiCounterfeitingUnifiedFileServiceImpl(AntiCounterfeitingUnifiedFileMapper antiCounterfeitingUnifiedFileMapper, FtpFactory ftpFactory, FtpConfig ftpConfig) {
        this.antiCounterfeitingUnifiedFileMapper = antiCounterfeitingUnifiedFileMapper;
        this.ftpFactory = ftpFactory;
        this.ftpConfig = ftpConfig;
    }

    /**
     * 上传文件并保存文件日志
     *
     * @param file
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AntiCounterfeitingUnifiedFile add(MultipartFile file) throws IOException, InterruptedException {
        AntiCounterfeitingUnifiedFile antiCounterfeitingUnifiedFile = new AntiCounterfeitingUnifiedFile();
        String newFileName = FileUploadUtils.extractFilename(file);
        String suffixName = FileUploadUtils.getExtension(file);
        antiCounterfeitingUnifiedFile.setFileName(file.getOriginalFilename());
        antiCounterfeitingUnifiedFile.setContentType(file.getContentType());
        String timeDir = DateUtils.dateTimeNow(DateUtils.YYYY_MM);
        String savePath = "/" + timeDir + "/" + suffixName;
        antiCounterfeitingUnifiedFile.setSavePath(savePath + "/" + newFileName);
        FtpConnection ftp = ftpFactory.getFtp();
        try {
            ftp.upload(savePath + "/" + newFileName, newFileName, file);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IOException();
        } finally {
            ftp.disconnect();
        }
        antiCounterfeitingUnifiedFileMapper.insertSelective(antiCounterfeitingUnifiedFile);
        return antiCounterfeitingUnifiedFile;
    }

    /**
     * 下载文件并保存文件日志
     *
     * @param fileId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void download(String fileId, String fileName, HttpServletResponse response) throws IOException, InterruptedException {
        String fileName2 = fileId.substring(fileId.lastIndexOf("/")+1);
        FtpConnection ftp = ftpFactory.getFtp();
        try {
            ftp.downloadFile(fileId, fileName2, response.getOutputStream());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IOException();
        } finally {
            ftp.disconnect();
        }
    }

}
